package com.mini.es.model;

import com.mini.common.EncryptField;
import com.mini.constant.YN;
import com.mini.vo.BusinessStudentInfoVO;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldNameConstants;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.Date;
import java.util.Objects;

/**
 * @author anjl
 * @title: BusinessStudentInfo
 * @date 2019/10/2711:28
 */
@Data
@FieldNameConstants
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = EsBusinessStudentOrderInfo.index)
public class EsBusinessStudentOrderInfo implements Serializable {
    public static final String index = "bussiness_student_order_info";
    private static final long serialVersionUID = -4856829255614103796L;
    /**
     * id
     */
    @Id
    private String orderNo;

    @ApiModelProperty(value = "组id")
    private BigInteger groupId;

    private Date addWechatTime;
    /**
     * 是否加微信了依赖addWechatTime，1加了 0未加
     */
    private Integer wechatCount;

    /**
     * 是否加微信了依赖addWechatTime，1加了 0未加
     */
    private Integer wechatCountByStates;

    @ApiModelProperty(value = "加群状态 0未加 1已加")
    private Integer addWechatGroupState;

    private Date admissionTestTime;

    /**
     * 依赖admissionTestTime，1是 0否
     */
    private Integer testCount;

    private Integer bigLessonNumThisWeek;

    private Integer bigLessonNumOtherWeek;

    private Float orderExpense;

    private Float orderPrice;

    private Float orderCost;

    private Float bigLessonFlowThisWeek;

    private Float bigLessonFlowOtherWeek;

    private Integer bigLessonRefundNum;

    private Float bigLessonRefundAmount;

    @Field(type = FieldType.Date)
    private Date orderUpdateTime;

    @Field(type = FieldType.Date)
    private Date attachUpdateTime;

    @Field(type = FieldType.Date)
    private Date teacherUpdateTime;

    @Field(type = FieldType.Date)
    private Date classUpdateTime;

    @Field(type = FieldType.Date)
    private Date baseUpdateTime;

    @Field(type = FieldType.Date)
    private Date updateTime;

    @Field(type = FieldType.Date)
    private Date mysqlUpdateTime;

    @Field(type = FieldType.Date)
    private Date createTime;

    @ApiModelProperty(value = "运营专员id")
    private BigInteger teacherId;

    @ApiModelProperty(value = "运营专员名称")
    private BigInteger teacherName;

    @ApiModelProperty(value = "业务线，0加盐业务线 1集团业务线")
    private Integer businessType;

    @ApiModelProperty(value = "课程id")
    private String lessonId;

    @ApiModelProperty(value = "小课业务ID")
    private BigInteger miniLessonId;

    @ApiModelProperty(value = "状态 1未加 2已加 ")
    private Integer states;

    @ApiModelProperty(value = "订单时间")
    @Field(type = FieldType.Date)
    private Date orderPayTime;

    /**
     * 2021-03-16 新增
     **/
    @ApiModelProperty(value = "是否使用优惠券 0未使用 1使用")
    private Integer getCoupon;

    @ApiModelProperty(value = "订单时间")
    @Field(type = FieldType.Date)
    private Date getCouponTime;

    @ApiModelProperty(value = "公众号关注标识 0未关注 1已关注")
    private Integer followPublic;

    @ApiModelProperty(value = "关注时间")
    private Date followPublicTime;

    @ApiModelProperty(value = "20200710增加 未支付0元单标识 0默认已支付 1未支付")
    private Integer unPayZeroState;


    /**
     * 2021-03-16 新增出勤
     **/
    @ApiModelProperty(value = "[出勤]orderNo")
    private String classOrderNo;

    @ApiModelProperty(value = "[出勤]出勤课节数-总")
    private Integer attnCount;

    @ApiModelProperty(value = "[出勤]出勤课节数-总-是否出勤 0否1是")
    private Integer attnCountExp;

    @ApiModelProperty(value = "[出勤]出勤课节数-直播")
    private Integer attnLiveCount;

    @ApiModelProperty(value = "[出勤]出勤课节数-直播-是否出勤 0否1是")
    private Integer attnLiveCountExp;

    @ApiModelProperty(value = "[出勤]出勤课节数-点播")
    private Integer attnVodCount;

    @ApiModelProperty(value = "[出勤]出勤课节数-点播-是否出勤 0否1是")
    private Integer attnVodCountExp;

    @ApiModelProperty(value = "[出勤]完课课节数-直播")
    private Integer finishLiveCount;

    @ApiModelProperty(value = "[出勤]直播是否完课-0否1是")
    private Integer finishLiveCountExp;

    @ApiModelProperty(value = "[出勤]完课课节数-总")
    private Integer finishCount;


    @ApiModelProperty(value = "[出勤]是否全部完课-0否1是")
    private Integer finishCountExp;

    @ApiModelProperty(value = "[出勤]班次下的课节数-总")
    private Integer classCourseCount;


    @ApiModelProperty(value = "[出勤]班次下的所有课节时长总和")
    private BigInteger classCourseDuration;

    @ApiModelProperty(value = "[出勤]直播时长")
    private BigInteger durationLiveCount;

    @ApiModelProperty(value = "[出勤]直播或点播时长（单个课节取时间长的算）")
    private BigInteger durationCount;

    private Integer changeClassState;

    @ApiModelProperty(value = "工作室id", required = true)
    private BigInteger workRoomId;

    @ApiModelProperty(value = "工作室名称", required = true)
    private String workRoomName;

    @ApiModelProperty(value = "渠道标识")
    private String channelCode;

    @ApiModelProperty(value = "渠道名称")
    private String channelName;

    @ApiModelProperty(value = "负责人id")
    private BigInteger fzId;

    @ApiModelProperty(value = "负责人名称")
    private String fzName;

    @ApiModelProperty(value = "手机号")
    @EncryptField
    private String phoneNo;

    @ApiModelProperty(value = "微信id")
    private String wxId;

    @ApiModelProperty(value = "学员id")
    private String userId;

    @ApiModelProperty(value = "姓名")
    private String nickName;

    @ApiModelProperty(value = "微信昵称")
    private String wechatName;

    @ApiModelProperty(value = "微信备注昵称")
    private String wechatNameNote;

    @ApiModelProperty(value = "课程标题")
    private String lessonTitle;

    public static EsBusinessStudentOrderInfo from(BusinessStudentInfoVO b) {

        EsBusinessStudentOrderInfo r = Objects.isNull(b) ? null : EsBusinessStudentOrderInfo.builder()
                .orderNo(b.getOrderNo())
                .groupId(b.getGroupId())
                .addWechatTime(b.getAddWechatTime())
                .wechatCount(Objects.isNull(b.getAddWechatTime()) ? 0 : 1)
                .wechatCountByStates(Integer.valueOf(2).equals(b.getStates()) ? 1 : 0)
                .addWechatGroupState(b.getAddWechatGroupState())
                .admissionTestTime(b.getAdmissionTestTime())
                .testCount(Objects.isNull(b.getAdmissionTestTime()) ? 0 : 1)
                .bigLessonNumThisWeek(b.getBigLessonNumThisWeek())
                .bigLessonNumOtherWeek(b.getBigLessonNumOtherWeek())
                .orderExpense(b.getOrderExpense())
                .orderPrice(b.getOrderPrice())
                .orderCost(b.getOrderCost())
                .bigLessonFlowThisWeek(b.getBigLessonFlowThisWeek())
                .bigLessonFlowOtherWeek(b.getBigLessonFlowOtherWeek())
                .bigLessonRefundNum(b.getBigLessonRefundNum())
                .bigLessonRefundAmount(b.getBigLessonRefundAmount())
                .orderUpdateTime(b.getOrderUpdateTime())
                .attachUpdateTime(b.getAttachUpdateTime())
                .teacherUpdateTime(b.getTeacherUpdateTime())
                .baseUpdateTime(b.getBaseUpdateTime())
                .createTime(b.getCreateTime())
                .teacherId(b.getTeacherId())
                .teacherName(b.getTeacherName())
                .businessType(b.getBusinessType())
                .lessonId(b.getLessonId())
                .lessonTitle(b.getLessonTitle())
                .miniLessonId(b.getMiniLessonId())
                .workRoomId(b.getWorkRoomId())
                .workRoomName(b.getWorkRoomName())
                .channelCode(b.getChannelCode())
                .channelName(b.getChannelName())
                .fzId(b.getFzId())
                .fzName(b.getFzName())
                .phoneNo(b.getPhoneNo())
                .wxId(b.getWxId())
                .userId(b.getUserId())
                .nickName(b.getNickName())
                .wechatName(b.getWechatName())
                .wechatNameNote(b.getWechatNameNote())
                .states(b.getStates())
                .orderPayTime(b.getOrderPayTime())
                .getCoupon(b.getGetCoupon())
                .getCouponTime(b.getGetCouponTime())
                .followPublic(b.getFollowPublic())
                .followPublicTime(b.getFollowPublicTime())
                .unPayZeroState(b.getUnPayZeroState())
                .changeClassState(Objects.nonNull(b.getChangeClassState()) ? Integer.parseInt(b.getChangeClassState().toString()) : null)
                .build();

        //出勤信息
        if (Objects.nonNull(r) && Objects.nonNull(b.getClassOrderNo())) {
            r.setClassOrderNo(b.getClassOrderNo());
            r.setClassUpdateTime(b.getClassUpdateTime());
            r.setAttnCount(b.getAttnCount());
            r.setAttnLiveCount(b.getAttnLiveCount());
            r.setAttnVodCount(b.getAttnVodCount());
            r.setFinishLiveCount(b.getFinishLiveCount());
            r.setFinishCount(b.getFinishCount());
            r.setClassCourseCount(b.getClassCourseCount());
            r.setDurationLiveCount(b.getDurationLiveCount());
            r.setDurationCount(b.getDurationCount());
            r.setClassCourseDuration(b.getClassCourseDuration());

            r.setAttnCountExp(Objects.nonNull(r.getAttnCount()) && r.getAttnCount() > 0 ? YN.YES.getCode() : YN.NO.getCode());
            r.setAttnVodCountExp(Objects.nonNull(r.getAttnVodCount()) && r.getAttnVodCount() > 0 ? YN.YES.getCode() : YN.NO.getCode());
            r.setAttnLiveCountExp(Objects.nonNull(r.getAttnLiveCount()) && r.getAttnLiveCount() > 0 ? YN.YES.getCode() : YN.NO.getCode());
            r.setFinishLiveCountExp(Objects.nonNull(r.getFinishLiveCount()) && Objects.nonNull(r.getClassCourseCount()) && r.getFinishLiveCount() >= r.getClassCourseCount() ? YN.YES.getCode() : YN.NO.getCode());
            r.setFinishCountExp(Objects.nonNull(r.getFinishCount()) && Objects.nonNull(r.getClassCourseCount()) && r.getFinishCount() >= r.getClassCourseCount() ? YN.YES.getCode() : YN.NO.getCode());
        }

        return r;
    }

}
